<template>
  <div class="directory-container">
    <div class="header">
      <span class="title">{{ props.title }}</span>
      <div class="toggle-btn" @click="toggleAll">
        {{ isAllExpanded ? '收起' : '展开' }}
        <SvgIcon icon-class="箭头右" />
      </div>
    </div>

    <div class="tree-container">
      <directory-node
          v-for="node in treeData"
          :key="node.id"
          :node="node"
          :depth="0"
          :selected-id="selectedId"
          @select="handleSelect"
      />
    </div>

    <div class="progress-container">
      <div class="progress-bar">
        <div class="progress-fill" :style="{ width: progressWidth + '%' }"/>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue';
import { TreeNode } from '@/types/treeType.ts';
import { getVisibleNodes, toggleAllNodes } from '@/utils/treeUtils.ts';
import DirectoryNode from "@/components/article/directory/DirectoryNode.vue";
import SvgIcon from "@/components/common/SvgIcon.vue";
const props = defineProps({
  title: {
    type: String,
    default: ''
  },
  treeData: {
    type: Array<TreeNode>,
    default:
        [{
          id: '1',
          title: '第一章',
          children: [
            {
              id: '1-1',
              title: '第一节',
              children: [
                { id: '1-1-1', title: '1.1.1' },
                { id: '1-1-2', title: '1.1.2' }
              ]
            },
            { id: '1-2', title: '第二节' }
          ]
        },
          {
            id: '2',
            title: '第二章',
            children: [
              { id: '2-1', title: '第一节' },
              { id: '2-2', title: '第二节' }
            ]
          }
        ]
  }
})

const treeData = ref<TreeNode[]>(props.treeData);
const selectedId = ref<string>('');
const isAllExpanded = ref(false);

// 计算属性
const visibleNodes = computed(() => getVisibleNodes(treeData.value));
const progressWidth = computed(() => {
  if (!selectedId.value) return 0;
  const index = visibleNodes.value.findIndex(n => n.id === selectedId.value);
  return ((index + 1) / visibleNodes.value.length) * 100;
});

// 方法
const toggleAll = () => {
  isAllExpanded.value = !isAllExpanded.value;
  treeData.value = toggleAllNodes(treeData.value, isAllExpanded.value);
};

const handleSelect = (node: TreeNode) => {
  selectedId.value = node.id;
}
</script>

<style lang="less" scoped>
.directory-container {
  background: #ffffff;
  border-radius: 8px;
  padding: 24px 24px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  overflow: hidden;
  position: relative;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;

  .title {
    font-weight: bold;
    font-size: 20px;
    line-height: 29px;
    // 限制文本只有一行
    overflow: hidden;
    //display: -webkit-box;
    text-overflow: ellipsis; // 超出部分显示省略号
    white-space: nowrap; // 强制不换行
    color: #333333;
    letter-spacing: 2px;
    margin: 0;
  }

  .toggle-btn {
    font-size: 14px;
    font-weight: 400;
    line-height: 20px;
    white-space: nowrap; // 强制不换行
    color: #666666;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 4px;
    transition: background 0.2s;
    display: flex;
    align-items: center;
    gap: 8px;

    &:hover {
      background: #f5f5f5;
    }
  }
}

.progress-container {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;

  .progress-bar {
    height: 6px;
    background:
    // 渐变背景 25% 宽度
        repeating-linear-gradient(
            to right,
            #e0e0e0 0,
            #e0e0e0 calc(25% - 3px),
            transparent calc(25% - 3px),
            transparent calc(25% + 3px),
            #e0e0e0 calc(25% + 3px)
        ),
        transparent;  // 背景色
    overflow: hidden;
    position: relative;

    .progress-fill {
      position: absolute;
      left: 0;
      top: 0;
      height: 100%;
      background: #eb5c88;
      transition: width 0.6s ease;
    }
  }
}
</style>
